#include "rowModel.h"

#include <QBrush>
#include <QTreeWidgetItem>
#include <QPixmap>
#include <QList>
#include <QString>

#include <KIcon>

#include <string>
#include <math.h>
#include <sys/stat.h>
#include <time.h>
#include <math.h>

#include <limits>

PackRow::PackRow(Pkg package){
  //setForeground(0, QBrush(Qt::gray));
  setForeground(1, QBrush(Qt::darkGray));
  //setForeground(2, QBrush(Qt::darkGray));
  setForeground(3, QBrush(Qt::gray));// il 3 deve ritornare 4 quando si useranno le icone
  //QString img = "../terminal.png"; // ora non ci servi

  QString date = QString(TimeDiff(package.GetLastAccess(), 'd').c_str());
  QString size = QString(package.GetField("Installed-Size").c_str());
  QString packname = QString(package.GetField("Package").c_str());

  QString infos;
  infos = QString(package.GetField("Short-Description").c_str()); 
  if(infos == ""){
    infos = QString(package.GetField("Version").c_str());
  }
  //setImage(img);
  setDate("  " + date + " ");
  setSize( "  " + size + " Kb  ");
  setPackName("  " + packname + "  ");
  setInfos(" "+ infos);
}

PackRow::~PackRow() {
}
void PackRow::setImage(QString img) {
//setIcon(3,KIcon(img));  // se img non e' nelle icone di sistema, metti quella di default
  const QPixmap pixMap = QPixmap(img);
  setIcon(3, pixMap);
}
void PackRow::setDate(QString date) {
  setText(0, date);
}
void PackRow::setSize(QString size) {
  setText(1, size);
}
void PackRow::setPackName(QString packname) {
  setText(2, packname);
}
void PackRow::setInfos(QString infos) {
  setText(3, infos); // il 3 deve ritornare 4 quando si useranno le icone
}

string PackRow::TimeDiff(time_t file_tm, const char m) {
/* d days, S sentence, D date */
  string toret;
  char timestr[100];

  time_t now;
  time(&now);
  double dif = difftime(now, file_tm);
  float retceil;

  if(file_tm==-1) { //il pacchetto non e' riuscito a fare una statistica degli accessi ai file
    toret=string("unavailable");
  }
  else if (m == 'd') {
    retceil=ceil(dif / (60 * 60 * 24));
  if(retceil>1) sprintf(timestr, "%.0f days", retceil);
  else sprintf(timestr, "today", retceil);
    toret = timestr;
  } 
  else if (m == 'D') {
    struct tm* atime = localtime(&file_tm);
    strftime(timestr, 100, "%Y-%m-%d", atime); //%X per l'ora esatta :D
    toret = timestr;
  }
  else if (m == 'S') {
    float fd=ceil(dif / (60 * 60 * 24));
    string timestr; 
    float timef;
    
    //float months=fd%30;
    //if(months<30) { }
    int cul = numeric_limits<int>::max();
    //timestr.sprintf("%.0f", timef);
    toret = timestr;
  }
  return toret;
}

bool PackRow::operator<(const QTreeWidgetItem &other)const {
// sovrascive il l'operatore < per ordinare le colonne come vogliamo       
  int SortCol = treeWidget()->sortColumn();
  Pkg pack1 = this->data(1,Qt::UserRole).value<Pkg>();
  Pkg pack2 = other.data(1,Qt::UserRole).value<Pkg>();
     
  if(SortCol==0){
    //if(pack2.GetLastAccess()==-1) return true; //xke nn mette i file a -1 (non trovati) oltre i numeri piu piccoli?
    if((pack1.GetLastAccess() < pack2.GetLastAccess()))
      return true; // lo metto dopo il pacchetto corrente
    else 
      return false; //lo metto qua
  }
  if(SortCol==1){
    if(atoi(pack1.GetField("Installed-Size").c_str()) < atoi(pack2.GetField("Installed-Size").c_str()))
      return true; // lo metto dopo
    else
      return false; //lo metto prima
  }
  else return text(SortCol).toLower() < other.text(SortCol).toLower();  
}


